/**
 *  Copyright 2006-2007 EmbedChat
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.embedchat.desktop.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class OptionsDialog extends JDialog {
	
	private static final long serialVersionUID = -2584034629339699061L;
	
	private JTextField serverHostValue = new JTextField("");
	private JTextField serverPortValue = new JTextField("");

	public OptionsDialog() {
		super((JFrame) null, "Connection options", true);
        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.BOTH;
        c.anchor = GridBagConstraints.FIRST_LINE_START;
        c.insets = new Insets(2,2,0,0);        
        panel.setPreferredSize(new Dimension(250, 150));
        panel.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(Color.BLACK),
                BorderFactory.createEmptyBorder(5,5,5,5)));
        serverHostValue.setPreferredSize(new Dimension(140, 25));
        serverPortValue.setPreferredSize(new Dimension(140, 25));
        JLabel serverHostLabel = new JLabel("server host:");
        JLabel serverPortLabel = new JLabel("server port:");
        c.gridx = 0;
        c.gridy = 0;
        panel.add(serverHostLabel, c);
        c.gridx = 1;
        c.gridy = 0;
        panel.add(serverHostValue, c);        
        c.gridx = 0;
        c.gridy = 1;
        panel.add(serverPortLabel, c);        
        c.gridx = 1;
        c.gridy = 1;
        panel.add(serverPortValue, c);        
        c.gridx = 0;
        c.gridy = 2;
        JButton button = new JButton("Ok");
        button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				dispose();
			}
        });
        c.gridx = 1;
        c.gridy = 2; 
        panel.add(button, c);
		add(panel);
	}
	
	public void setInitValues(final String currentHost, final int currentPort) {
		serverHostValue.setText(currentHost);
		serverPortValue.setText(String.valueOf(currentPort));
	}

	public String getServerHost() {
		return serverHostValue.getText();
	}
	
	public int getServerPort() {
		try {
			return Integer.valueOf(serverPortValue.getText());			
		} catch (NumberFormatException e) {
			return 0;
		}
	}
	
//	public static void main(String[] args) {
//		OptionsDialog dialog = new OptionsDialog(new JFrame());
//		dialog.setInitValues("localhost", 6767);
//		dialog.pack();
//		dialog.setVisible(true);
//		log.debug("host: " + dialog.getServerHost());
//		log.debug("port: " + dialog.getServerPort());
//	}
}
